package com.zjhhsd.blackJack.controller;

import com.zjhhsd.blackJack.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
public class RegisterController {

    @Autowired
    private UserMapper userMapper;

    @GetMapping("/register")
    public String toRegister(
            HttpServletRequest request
    ){
        System.out.println("get方法进入了注册页面");
        request.getSession().setAttribute("wrong",null);
        return "register";
    }

    @PostMapping("/register")
    public String register(
            @RequestParam("name") String name,
            @RequestParam("password") String password,
            HttpServletResponse response,
            HttpServletRequest request,
            Model model
    ){
        //账户名去除空格
        name = name.replaceAll(" ","");
        if(userMapper.getByName(name)!=null){
            request.getSession().setAttribute("wrong","exist");
            return "register";
        }
        userMapper.insert(name,password);
        request.getSession().setAttribute("success",name);
        return "redirect:/login";
    }
}
